package com.hj.service.impl;

import com.hj.domain.LoginUser;
import com.hj.domain.entity.Role;
import com.hj.domain.entity.User;
import com.hj.util.CypherUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 登录处理
 * @author hj
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private CypherUtil cypherUtil;


    @Override
    public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
        User user1 = new User();
        user1.setMobile("18820864611");
        // base64 加密的 123456
        user1.setPassword("MTIzNDU2");
        User user2 = new User();
        user2.setMobile("18820864612");
        user2.setPassword("MTIzNDU2");
        User user3 = new User();
        user3.setMobile("18820864613");
        user3.setPassword("MTIzNDU2");
        List<User> users = Arrays.asList(user1, user2, user3);
        final boolean b = users.stream().anyMatch(u -> u.getMobile().equals(mobile));
        if (!b) {
            throw new UsernameNotFoundException("用户不存在");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setMobile(cypherUtil.aesEncode("18820864611"));
        Role role1 = new Role();
        role1.setId(1L);
        role1.setName("ROLE_common");
        Role role2 = new Role();
        role1.setId(2L);
        role2.setName("ROLE_admin");
        List<Role> roles = Arrays.asList(role1, role2);
        loginUser.setRoles(roles);
        return loginUser;
    }

}
